package com.star.carservice.client.fragment.parts;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.baidu.location.BDLocation;
import com.star.carservice.R;
import com.star.carservice.client.AppController;
import com.star.carservice.client.fragment.base.BaseFragment;
import com.star.carservice.client.utils.AsyncTaskUtils;
import com.star.carserviceapi.domain.BannerInfo;
import com.star.carserviceapi.domain.request.QryWeatherRequest;
import com.star.carserviceapi.domain.result.QryBannersResult;
import com.star.carserviceapi.domain.result.QryWeatherResult;
import com.squareup.picasso.Picasso;
import com.viewpagerindicator.CirclePageIndicator;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class BannerFragment extends BaseFragment {

	private final static String TAG = BannerFragment.class.getSimpleName();

	@InjectView(R.id.pager)
	ViewPager pager;

	@InjectView(R.id.indicator)
	CirclePageIndicator indicator;

	BannerPagerAdapter mBannerPagerAdapter;
	private List<BannerInfo> mItems = new ArrayList<BannerInfo>();

	@Override
	public boolean IsRetainInstance() {
		return false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// execAsyncTask(new LoadBannerTask());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (CacheViewIsNull()) {
			mItems.add(new BannerInfo());

			mCacheView = inflater.inflate(R.layout.frag_banner, container,
					false);

			ButterKnife.inject(this, mCacheView);

			mBannerPagerAdapter = new BannerPagerAdapter(
					getChildFragmentManager(), mItems);
			pager.setAdapter(mBannerPagerAdapter);
			indicator.setViewPager(pager);
		}

		removeCacheView();
		return mCacheView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		super.onActivityCreated(savedInstanceState);

	}

	@Override
	public String getLogTag() {
		return TAG;
	}

	class LoadBannerTask extends AsyncTask<Void, Void, QryBannersResult> {

		@Override
		protected QryBannersResult doInBackground(Void... arg0) {
			return AppController.getInstance().getApi().qryBanners();
		}

		@Override
		protected void onPostExecute(QryBannersResult result) {

			if (result.successful()) {
				mItems.clear();
				mItems.add(new BannerInfo());

				result.getBannerItems().add(0,
						new BannerInfo(R.drawable.banner_weather));

				mItems.addAll(result.getBannerItems());
				mBannerPagerAdapter.notifyDataSetChanged();
			}
		}
	}

	static class BannerPagerAdapter extends FragmentStatePagerAdapter {

		private List<BannerInfo> mData = null;

		public BannerPagerAdapter(FragmentManager fm, List<BannerInfo> data) {
			super(fm);
			this.mData = data;
		}

		@Override
		public Fragment getItem(int postion) {

			if (postion == 0) {
				return new WeatherFragment();
			}

			return BannerItemFragment.newInstance(mData.get(postion));
		}

		@Override
		public int getCount() {

			return mData.size();
		}
	}

	static class BannerItemFragment extends BaseFragment {

		private final static String Key_BannerItem = "BannerItem";

		public static BannerItemFragment newInstance(BannerInfo item) {
			BannerItemFragment fragment = new BannerItemFragment();

			Bundle bundle = new Bundle();

			bundle.putSerializable(Key_BannerItem, item);

			fragment.setArguments(bundle);

			return fragment;
		}

		private final static String TAG = BannerItemFragment.class
				.getSimpleName();

		@InjectView(R.id.img_banner)
		ImageView img_banner;

		@Override
		public boolean IsRetainInstance() {
			return false;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			super.onCreateView(inflater, container, savedInstanceState);

			View view = inflater.inflate(R.layout.frag_banner_weather,
					container, false);

			ButterKnife.inject(this, view);

			return view;
		}

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);

			BannerInfo item = (BannerInfo) getArguments().getSerializable(
					Key_BannerItem);

			if (item.isLocalRes()) {

				Picasso.with(getActivity()).load(item.getImgResId())
						.into(img_banner);
			} else {
				Picasso.with(getActivity()).load(item.getImgUrl())
						.into(img_banner);
			}
		}

		@Override
		public String getLogTag() {
			return TAG;
		}
	}

	static class WeatherFragment extends BaseFragment {

		@Override
		public String getLogTag() {

			return WeatherFragment.class.getSimpleName();
		}

		@InjectView(R.id.img_banner)
		ImageView img_banner;

		@InjectView(R.id.lbl_temperature)
		TextView lbl_temperature;

		@InjectView(R.id.lbl_desc)
		TextView lbl_desc;

		@InjectView(R.id.lbl_wash)
		TextView lbl_wash;

		@InjectView(R.id.lbl_city)
		TextView lbl_city;

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);

			loadWeather();
		}

		@Override
		public boolean IsRetainInstance() {
			return false;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			super.onCreateView(inflater, container, savedInstanceState);

			if (CacheViewIsNull()) {
				mCacheView = inflater.inflate(R.layout.frag_banner_weather,
						container, false);

				ButterKnife.inject(this, mCacheView);
			}

			removeCacheView();

			return mCacheView;
		}

		@OnClick(R.id.lbl_city)
		public void onRefreshWeather(View v) {
			loadWeather();
		}

		private void loadWeather() {
			BDLocation mLastDBLocation = AppController.getInstance()
					.getLastDBLocation();

			if (mLastDBLocation == null)
				return;

			execAsyncTask(new QryWeather(), new QryWeatherRequest(
					mLastDBLocation.getCity()));
		}

		class QryWeather extends
				AsyncTask<QryWeatherRequest, Void, QryWeatherResult> {

			@Override
			protected void onPreExecute() {
				if (lbl_temperature != null) {
					lbl_temperature.setText("");
					lbl_desc.setText("");
					lbl_wash.setText("");
					lbl_city.setText("");
				}
			}

			@Override
			protected QryWeatherResult doInBackground(
					QryWeatherRequest... params) {
				return getApi().qryWeather(params[0]);
			}

			@Override
			protected void onPostExecute(QryWeatherResult result) {

				if (result.successful()) {
					lbl_city.setText(AppController.getInstance()
							.getLastDBLocation().getCity());

					lbl_temperature.setText(result.getData().getTemperature());
					lbl_desc.setText(result.getData().getWind() + " "
							+ result.getData().getWeather());
					lbl_wash.setText(result.getData().getWash());
				}

			}
		}

	}
}
